/**
 * Created by Apple on 2016/7/13.
 */

'use strict';

import SqlPool from './sql_pool.js';

/**
 * 不想去了解 thinkjs 的db了， 直接自己搞个工具去读取数据好了。
 */
export default new class DBHelper extends think.controller.base {

    init() {
        super.init();
        console.log("DBHelper 初始化");
        this.db = SqlPool;
    }


    /**
     * 获取房间列表
     * @returns {promise|Promise|*}
     */
    getRoomList() {

        return this.db.x_query('call bg_room_list();');
    }

    /**
     * 得到所有的充值记录
     */
    getRechargeRecord(startTime, endTime, startIndex, size, userID, status, channel) {
        if (status == "0")
            status = null;

        return this.db.x_query('call bg_recharge_record(?, ?, ?, ?, ?, ?, ?)', [startTime, endTime, startIndex, size, userID, status, channel]);
    }

    getRechargeStat(startTime, endTime, startIndex, size, type, channel) {
        return this.db.x_query('call bg_recharge_stat(?, ?, ?, ?, ?, ?)', [startTime, endTime, startIndex, size, type, channel]);
    }

    //获取
    getRechargeRecordByOrderID(orderID) {
        return this.db.x_query("select * from ps_pay where payOrder = ?", [orderID]);
    }

    //修复 bug
    fixRechargeByOrderID(orderID) {
        return this.db.x_query("call bg_fix_recharge_by_orderID(?)", [orderID]);
        // return this.db.x_query("update ps_pay set `status` = 200 where `status` = 100 and payOrder = ?" , [orderID]);
    }

    /**
     * 得到反馈列表
     * @param startIndex
     * @param limitSize
     * @returns {*}
     */
    getFeedback(startIndex, limitSize, gameID) {
        return this.db.x_query('call bg_feedback(?, ?,?)', [startIndex, limitSize, gameID]);
    }

    /**
     * 得到所有的公告信息
     * @returns {promise|*|Promise}
     */
    getBroadcast(startTime, endTime, startIndex, size) {
        return this.db.x_query("select * from ps_broadcast;");
    }

    getAnyShareReward(startTime, endTime, startIndex, size, userID) {
        return this.db.x_query("call bg_any_share_reward(?,?,?,?,?)", [startTime, endTime, userID, startIndex, size]);
    }

    /**
     * 得到所有的敏感词
     * @returns {promise|*|Promise}
     */
    getDirtyWords(startIndex, searchSize) {
        return this.db.x_query("select * from ps_dirty_words order by id limit ?,?", [Number(startIndex), Number(searchSize)]);
    }

    /**
     * 得到所有的游戏模块
     * @returns {promise|*|Promise}
     */
    getGameModule() {
        return this.db.x_query("select * from ps_game_module order by sortID;");
    }

    /**
     * 得到玩家在各个房间的收入情况
     */
    getUserRoomModuleRevenue(startTime, endTime, userID) {

        return this.db.x_query("call bg_user_room_revenue(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 得到指定userID的信息
     * @param userID
     * @returns {*|Promise|promise}
     */
    getUserInfo(userID) {
        return this.db.x_query("call bg_user_info(?)", [userID]);
    }

    /**
     * 得到指定时间内的登录记录
     */
    getLoginLog(startTime, endTime, startIndex, size, userID, ip, channel) {
        return this.db.x_query("call bg_user_login_log(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, size, userID, ip, channel]);
    }

    /**
     * 得到指定时间内的注册记录
     */
    getRegLog(startTime, endTime, startIndex, size, ip, deviceID, channel) {
        return this.db.x_query("call bg_user_reg_log(?, ?, ?, ?, ?, ?, ?)", [startTime, endTime, startIndex, size, ip, deviceID, channel]);
    }

    /**
     * 得到指定时间内的系统收入
     */
    getTotalRevenue(startTime, endTime) {
        return this.db.x_query("call bg_total_revenue(?, ?)", [startTime, endTime]);
    }

    /**
     * 提现总数
     */
    getPayCashNum(startTime, endTime, userID) {

        if (userID != null) {
            return this.db.x_query("select sum(money) as payCashNum from ps_pay_cash_log where userID = ? and result = 1 and ts between ? and ?", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select sum(money) as payCashNum from ps_pay_cash_log where result = 1 and ts between ? and ?", [startTime, endTime]);
        }

    }


    /**
     * 获取红包记录数
     */
    getLuckyRmbLogCount(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select count(*) as count from ps_lucky_rmb_log where userID = ? and ts between ? and ?", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select count(*) as count from ps_lucky_rmb_log where ts between ? and ?", [startTime, endTime]);
        }
    }

    /**
     * 获取邮件记录数
     */
    getUserMailLogCount(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select count(*) as count from ps_mail where userID = ? and createTime between ? and ?", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select count(*) as count from ps_mail where createTime between ? and ?", [startTime, endTime]);
        }
    }

    /**
     * 获取摊位记录数
     */
    getStoreTradeLogCount(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select count(*) as count from ps_user_store_trade_log where (buyerID = ? or sellerID = ?) and ts between ? and ?", [userID, userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select count(*) as count from ps_user_store_trade_log where ts between ? and ?", [startTime, endTime]);
        }
    }


    /**
     * 获取红包记录变化金额数
     */
    getLuckyRmbLogSum(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select sum(money) as sum from ps_lucky_rmb_log where userID = ? and ts between ? and ?", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select sum(money) as sum from ps_lucky_rmb_log where ts between ? and ?", [startTime, endTime]);
        }
    }

    /**
     * 领取推广红包
     */
    getTakeRmbNum(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select sum(luckyRMB) as takeRmbNum from ps_wx_once_lucky_rmb where userID = ? and ts between ? and ?", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select sum(luckyRMB) as takeRmbNum from ps_wx_once_lucky_rmb where ts between ? and ?", [startTime, endTime]);
        }

    }

    /**
     * 获取红包雨发送总额
     */
    getRedPackRainNum(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select sum(money) as rprNum from ps_red_pack_rain_log where userID = ? and ts between ? and ?", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select sum(money) as rprNum from ps_red_pack_rain_log where ts between ? and ?", [startTime, endTime]);
        }

    }

    /**
     * 得到指定时间内的话费支出
     */
    getHuaFei(startTime, endTime, userID) {
        if (userID != null) {
            return this.db.x_query("select sum(orderCash)as allCash, sum(num) as allNum from ps_mobile_fare where `status` = 1 and userID = ? and ts between ? and ?;", [userID, startTime, endTime]);
        }
        else {
            return this.db.x_query("select sum(orderCash)as allCash, sum(num) as allNum from ps_mobile_fare where `status` = 1 and ts between ? and ?;", [startTime, endTime]);
        }

    }


    /**
     * 得到当前登录用户
     * @param startIndex
     * @param size
     * @returns {*}
     */
    getOnlineUser(startIndex, size) {
        return this.db.x_query("call bg_online_user(?, ?)", [startIndex, size]);
    }


    /**
     * 得到用户详细游戏记录
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param size
     * @param userID
     * @param roomID
     * @param moduleID
     * @returns {*}
     */
    getUserGameDetailed(startTime, endTime, startIndex, size, userID, roomID, moduleID, channel) {
        return this.db.x_query("call bg_user_game_detailed(?,?,?,?,?,?,?,?)", [startTime, endTime, startIndex, size, userID, roomID, moduleID, channel]);
    }

    getUserGameStat(startTime, endTime, startIndex, size, type, channel) {
        return this.db.x_query("call bg_user_game_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, size, type, channel]);
    }

    /**
     * 金币排行
     * @param startIndex
     * @param limitSize
     * @returns {*}
     */
    getMoneyRank(startIndex, limitSize) {
        return this.db.x_query("call bg_money_rank(?,?)", [startIndex, limitSize]);
    }

    /**
     * 代理金币排行
     * @param startIndex
     * @param limitSize
     * @returns {*}
     */
    getAgentMoneyRank(startIndex, limitSize) {
        return this.db.x_query("call bg_agent_money_rank(?,?)", [startIndex, limitSize]);
    }

    /**
     * 玩家身上红包排行
     * @param startIndex
     * @param limitSize
     */
    getLuckyRmbRank(startIndex, limitSize) {
        return this.db.x_query("select userID, gameID, nickname, luckyRMB, score, bankScore, payGoodsRMB, paySum from ps_account where isAndroid = 0 and `enable` = 1 order by luckyRMB desc limit ?,?", [startIndex, limitSize]);
    }

    /**
     * 钻石排行
     * @param startIndex
     * @param limitSize
     * @returns {*}
     */
    getAcerRank(startIndex, limitSize) {
        return this.db.x_query("call bg_acer_rank(?,?)", [startIndex, limitSize]);
    }


    getPrizeCodeLog(startIndex, limitSize, userID, money) {
        return this.db.x_query("call bg_prize_log(?,?,?,?)", [startIndex, limitSize, userID, money]);
    }

    /**
     * 注册统计
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param limitSize
     */
    getRegStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_reg_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    /**
     * 登录统计
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param limitSize
     */
    getLoginStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_login_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    /**
     * 用户的详细银行记录
     */
    getUserBankDetailed(startTime, endTime, startIndex, size, userID, quota, type, channel) {
        return this.db.x_query("call bg_user_bank_detailed(?,?,?,?,?,?,?,?)", [startTime, endTime, startIndex, size, userID, quota, type, channel]);
    }

    /**
     * 代理的详细银行记录
     */
    getAgentBankDetailed(startTime, endTime, startIndex, size, userID, quota, type, channel) {
        return this.db.x_query("call bg_agent_bank_detailed(?,?,?,?,?,?,?,?)", [startTime, endTime, startIndex, size, userID, quota, type, channel]);
    }

    /**
     * 用户的银行统计
     */
    getUserBankSum(startTime, endTime, userID, channel) {
        return this.db.x_query("call bg_user_bank_sum(?,?,?,?)", [startTime, endTime, userID, channel]);
    }

    /**
     * 代理的银行统计
     */
    getAgentBankSum(startTime, endTime, userID, channel) {
        return this.db.x_query("call bg_agent_bank_sum(?,?,?,?)", [startTime, endTime, userID, channel]);
    }

    /**
     * 代理的转账统计
     */
    getAgentTransferStat(startTime, endTime) {
        return this.db.x_query("call bg_agent_transfer_stat(?,?)", [startTime, endTime]);
    }

    //用户返利查询2
    getUserRebate2(startTime, endTime, userID) {
        return this.db.x_query("call bg_user_rebate_count(?,?,?)", [startTime, endTime, userID]);
    }

    //查询用户身上商品
    getUserGoods(startIndex, limitSize, goodsID, userID) {
        return this.db.x_query("call bg_user_goods(?,?,?,?)", [startIndex, limitSize, goodsID, userID]);
    }

    //道具购买记录
    getUserPayGoodsLog(startTime, endTime, startIndex, limitSize, userID, goodsID, channel) {
        return this.db.x_query("call bg_user_pay_goods_log(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, goodsID, channel]);
    }

    //道具购买统计
    getUserPayGoodsStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_pay_goods_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //邮件记录
    getUserMailLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_user_mail_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //邮件统计
    getUserMailStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_mail_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //摊位记录
    getUserStoreLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_user_store_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //摊位统计
    getUserStoreStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_store_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //交易记录
    getUserStoreTradeLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_user_store_trade_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //交易统计
    getUserStoreTradeStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_store_trade_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //聊天记录
    getUserChatLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_user_chat_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //聊天统计
    getUserChatStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_chat_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //红包记录
    getLuckyRmbLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_lucky_rmb_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //领取推广红包记录
    getTakeLuckyRmbLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_lucky_rmb_take_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //提现统计, type 1真实姓名， 2  userID, 3 微信或支付宝， 4渠道
    getPayCashStat(startTime, endTime, startIndex, limitSize, type) {
        return this.db.x_query("call bg_pay_cash_stat(?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type]);
    }

    //提现记录
    getPayCashLog(startTime, endTime, startIndex, limitSize, userID, result, channel) {
        return this.db.x_query("call bg_pay_cash_log(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, result, channel]);
    }

    //红包统计
    getLuckyRmbStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_lucky_rmb_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //用户话费充值记录
    getMobileFareLog(startTime, endTime, startIndex, limitSize, userID, status, channel) {
        return this.db.x_query("call bg_mobile_fare_log(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, status, channel]);
    }

    //用户话费充值统计
    getMobileFareSum(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_mobile_fare_sum(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //道具使用记录
    getUserUseGoodsLog(startTime, endTime, startIndex, limitSize, userID, goodsID, channel) {
        return this.db.x_query("call bg_user_use_goods_log(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, goodsID, channel]);
    }

    //道具使用统计
    getUserUseGoodsStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_user_use_goods_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    /**
     * 用户收入排行
     * @param startTime
     * @param endTime
     * @returns {*}
     */
    getUserAllRevenue(startTime, endTime, startIndex, size, userID, roomID, desc) {
        return this.db.x_query("call bg_user_all_revenue(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, size, userID, roomID, desc]);
    }

    //读取所有的用户表
    readAccounts() {
        return this.db.x_query("call bg_read_accounts()");
    }

    //更新玩家的微信公众号OpenID
    updateWXGZHOpenID(unionID, openID) {
        return this.db.x_query("update ps_account set wxgzhOpenID = ? where wxUnionID = ?", [openID, unionID]);
    }


    /**
     * 微信公众号 推荐关注
     * @param unionID
     * @param nickname
     * @param sharerUnionID
     * @param sharerNickname
     * @returns {*}
     */
    wxRecommendSubscribe(unionID, nickname, sharerUnionID, sharerNickname) {
        return this.db.x_query("call bg_wx_subscribe(?,?,?,?)", [unionID, nickname, sharerUnionID, sharerNickname]);
    }

    /**
     * 用户登录
     * @param account
     * @param password
     * @returns {*}
     */
    bgAccountLogin(account, password) {
        return this.db.x_query("call bg_login(?,?);", [account, password]);
    }

    /**
     * 查询封禁用户
     */
    getUserBan(startTime, endTime, startIndex, size, channel) {
        return this.db.x_query("call bg_user_ban(?,?,?,?,?)", [startTime, endTime, startIndex, size, channel]);
    }


    /**
     * 查询在线人数
     */
    getOnlineNum() {
        return this.db.x_query("select count(*) as onlineNum from ps_user_login_log where logoutTs = 0 ");
    }

    /**
     * 获取当前玩家的全部金币
     * @returns {*}
     */
    getAllGold() {
        return this.db.x_query("select sum(score+bankScore) as allGold from ps_account where isAndroid = 0 and `enable` = 1");
    }

    /**
     * 获取当前代理的全部金币
     * @returns {*}
     */
    getAgentGold() {
        return this.db.x_query("select sum(score+bankScore) as allGold from ps_account where isAndroid = 0 and `enable` = 1 and (memberOrder = 9 or userID in (select userID from( \
           select userID, sum(money) as moneySum from ps_money_detail where type = 6 group by userID \
       ) A where moneySum > 20000000))");
    }

    /**
     * 获取当前玩家的全部钻石
     * @returns {*}
     */
    getAllAcer() {
        return this.db.x_query("select sum(acer+bankAcer) as allAcer from ps_account where isAndroid = 0 and `enable` = 1");
    }

    /**
     * 获取当前玩家的全部推广红包
     * @returns {*}
     */
    getAllLuckyRmb() {
        return this.db.x_query("select sum(luckyRMB) as allLuckyRmb from ps_account where isAndroid = 0 and `enable` = 1");
    }

    /**
     * 查询一段时间的活跃人数
     * @param startTime
     * @param endTime
     * @returns {*}
     */
    getActiveNum(startTime, endTime) {
        return this.db.x_query("call bg_active_num(?,?)", [startTime, endTime]);
    }

    /**
     * 查询一段时间的IP数
     * @param startTime
     * @param endTime
     * @returns {*}
     */
    getIPNum(startTime, endTime) {
        return this.db.x_query("call bg_ip_num(?,?)", [startTime, endTime]);
    }

    /**
     * 查询一段时间  注册赠送总金币
     * @param startTime
     * @param endTime
     * @param ip
     * @returns {*}
     */
    getZCMoney(startTime, endTime, ip) {
        return this.db.x_query("call bg_zc_money(?,?,?)", [ip, startTime, endTime]);
    }

    /**
     * 得到充值总数
     * @param userID
     * @param startTime
     * @param endTime
     * @returns {*}
     */
    getCZNum(userID, startTime, endTime) {
        return this.db.x_query("call bg_cz_num(?,?,?)", [userID, startTime, endTime]);
    }

    /**
     * 查询一段时间的注册人数
     * @param startTime
     * @param endTime
     * @returns {*}
     */
    getRegisterNum(startTime, endTime) {
        return this.db.x_query("call bg_register_num(?,?)", [startTime, endTime]);
    }

    /**
     *
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param size
     */
    getActiveUser(startTime, endTime, startIndex, size, channel) {
        return this.db.x_query("call bg_active_user(?,?,?,?,?)", [startTime, endTime, startIndex, size, channel]);
    }

    /**
     * 查询用户返利
     * @param startIndex
     * @param size
     * @returns {*}
     */
    getUserRebate(startIndex, size) {
        return this.db.x_query("call bg_user_rebate(?,?)", [startIndex, size]);
    }

    /**
     * 查询受控用户
     * @param startIndex
     * @param size
     * @returns {*}
     */
    getUserControl(startIndex, size) {
        return this.db.x_query("call bg_user_control(?,?)", [startIndex, size]);
    }

    /**
     * 查询管理员赠送金币记录
     * @returns {*}
     */
    getGiveMoneyLog(startTime, endTime, startIndex, size, userID) {
        return this.db.x_query("call bg_give_money_log(?,?,?,?,?)", [startTime, endTime, startIndex, size, userID]);
    }


    /**
     * 查询管理员赠送VIP记录
     * @returns {*}
     */
    getGiveVipLog(startTime, endTime, startIndex, size, userID) {
        return this.db.x_query("call bg_give_vip_log(?,?,?,?,?)", [startTime, endTime, startIndex, size, userID]);
    }

    /**
     * 查询红包雨活动获奖用户
     * @returns {*}
     */
    getRedPackRainLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_red_pack_rain_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    /**
     * 查询某用户的在线时长
     * @param startTime
     * @param endTime
     * @param userID
     * @returns {*}
     */
    getOnlineTime(startTime, endTime, userID) {
        return this.db.x_query("call bg_online_time(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 获取有效分享数
     * @param startTime
     * @param endTime
     * @param userID
     * @returns {*}
     */
    getValidShareNum(startTime, endTime, userID) {
        return this.db.x_query("call bg_share_valid_share_num(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 得到一段时间的分享奖励和
     * @param startTime
     * @param endTime
     * @param userID
     * @returns {*}
     */
    getRewardSum(startTime, endTime, userID) {
        return this.db.x_query("call bg_share_reward_sum(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 查询一段时间的分享有效点击数
     * @param startTime
     * @param endTime
     * @param userID
     * @returns {*}
     */
    getShareValidClickNum(startTime, endTime, userID) {
        return this.db.x_query("call bg_share_valid_click_num(?,?,?)", [startTime, endTime, userID]);
    }


    /**
     * 查询一段时间的分享有效点击数
     * @param startTime
     * @param endTime
     * @param userID
     * @returns {*}
     */
    getAnyShareValidClickNum(startTime, endTime, userID) {
        return this.db.x_query("call bg_any_share_valid_click_num(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 获取银商净销量
     * @param startTime
     * @param endTime
     * @param userID
     */
    getNetSales(startTime, endTime, userID) {
        return this.db.x_query("call bg_net_sales(?,?,?)", [startTime, endTime, userID]);
    }


    /**
     * 获取银商全部
     * @param startTime
     * @param endTime
     * @param userID
     */
    getNetAll(startTime, endTime, userID) {
        return this.db.x_query("call bg_net_all(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 获取银商与其它银商间的转入转出总和
     * @param startTime
     * @param endTime
     * @param userID
     */
    getNetNotSales(startTime, endTime, userID) {
        return this.db.x_query("call bg_net_not_sales(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 获取银商与其它银商间的转入转出绝对值的总和
     * @param startTime
     * @param endTime
     * @param userID
     */
    getNetAbsSales(startTime, endTime, userID) {
        return this.db.x_query("call bg_net_abs_sales(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 获取任意分享 总和
     * @param startTime
     * @param endTime
     * @param userID
     * @returns {*}
     */
    getAnyShareRewardSum(startTime, endTime, userID) {
        return this.db.x_query("call bg_any_share_reward_sum(?,?,?)", [startTime, endTime, userID]);
    }

    /**
     * 得到红包大派送的记录
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param limitSize
     * @param userID
     * @returns {*}
     */
    getActivityLuckyMoneyLog(startTime, endTime, startIndex, limitSize, userID) {
        return this.db.x_query("call bg_activity_lucky_money_log(?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID]);
    }

    /**
     * 得到红包大派送统计
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param limitSize
     * @param userID
     * @returns {*}
     */
    getActivityLuckyMoneyStat(startTime, endTime, startIndex, limitSize, userID) {
        return this.db.x_query("call bg_activity_lucky_money_stat(?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID]);
    }

    /**
     * 得到榜单活动记录
     * @param startTime
     * @param endTime
     * @param startIndex
     * @param limitSize
     * @param userID
     * @param type
     * @param group
     * @returns {*}
     */
    getActivityRankingLog(startTime, endTime, startIndex, limitSize, userID, type, group) {
        return this.db.x_query("call bg_activity_ranking_log(?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, type, group]);
    }


    /**
     * 保存管理员操作,
     * @param id
     * @param adminID
     * @param type
     * @param status
     * @param detailed
     */
    recordAdminOP(adminID, type, status, detailed) {
        return this.db.x_query("call bg_record_admin_op(?,?,?,?)", [adminID, type, status, detailed]);
    }

    /**
     * 更新管理员操作
     * @param id
     * @param status
     */
    updateAdminOp(id, status) {
        return this.db.x_query("update bg_op_log set `status` = ? where id = ?", [status, id]);
    }

    /**
     * 查询管理员操作
     * @param adminID
     * @param type
     * @param status
     * @param startTime
     * @param endTime
     * @param start
     * @param size
     */
    selectAdminOp(adminID, type, status, startTime, endTime, start, size) {
        return this.db.x_query("call bg_select_admin_op(?, ?, ?, ?, ?, ?, ?)", [adminID, type, status, startTime, endTime, start, size]);
    }

    //查询库存及抽水
    selectStockAndTaxSE(startTime, endTime, roomID) {
        return this.db.x_query("call bg_stock_tax_s_e(?,?,?)", [startTime, endTime, roomID]);
    }

    //查询库存及抽水
    selectStockAndTax(startTime, endTime, roomID, startIndex, limitSize) {
        return this.db.x_query("call bg_stock_tax(?,?,?,?,?)", [startTime, endTime, roomID, startIndex, limitSize]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 获取喇叭
     * @param start
     * @param size
     * @returns {*}
     */
    getHorn(start, size) {
        return this.db.x_query("call bg_get_horn(?, ?)", [start, size]);
    }

    //删除喇叭
    removeHorn(id) {
        return this.db.x_query("delete from ps_broadcast where broadcastID = ?", [id]);
    }

    //修改喇叭
    editHorn(id, message) {
        return this.db.x_query("update ps_broadcast set message = ?,createTime = CURRENT_TIMESTAMP where broadcastID = ?", [message, id]);
    }

    //增加喇叭
    addHorn(message) {
        return this.db.x_query("insert into ps_broadcast(message,`trigger`) values(?,'0 */5 * * * *')", [message]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 获取公告
     * @returns {*}
     */
    getAnnouncement() {
        return this.db.x_query("select * from ps_game_announcement");
    }

    //删除公告
    removeAnnouncement(id) {
        return this.db.x_query("delete from ps_game_announcement where id = ?", [id]);
    }

    //修改公告
    editAnnouncement(id, announcement, link, startTime, endTime) {
        return this.db.x_query("update ps_game_announcement set announcement = ?,createTime = CURRENT_TIMESTAMP,`link` = ?,startTime=?,endTime=? where id = ?", [announcement, link, startTime, endTime, id]);
    }

    //增加公告
    addAnnouncement(announcement, link, startTime, endTime) {
        return this.db.x_query("insert into ps_game_announcement(announcement,`link`, startTime, endTime) values(?,?,?,?)", [announcement, link, startTime, endTime]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////
    //获取登录记录
    getOnlineChart(startTime, endTime) {

        return this.db.x_query("call bg_online_chart(?,?)", [startTime, endTime]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////
    //分享活动相关查询

    //获取分享订单
    getShareOrder(startTime, endTime, startIndex, limitSize, orderID, groupID, status, wxID) {

        return this.db.x_query("call bg_share_order(?,?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, orderID, groupID, status, wxID]);
    }

    //分享订单被点击记录查询
    getShareVisitLog(startTime, endTime, startIndex, limitSize, orderID, groupID, ip, valid) {

        return this.db.x_query("call bg_share_visit(?,?,?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, orderID, groupID, ip, valid]);
    }

    //任意分享被点击记录查询
    getAnyShareVisitLog(startTime, endTime, startIndex, limitSize, ip, valid) {
        return this.db.x_query("call bg_any_share_visit(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, ip, valid]);
    }

    //分享奖励查询
    getShareRewardLog(startTime, endTime, startIndex, limitSize, userID, shareDay) {
        return this.db.x_query("call bg_share_reward_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, shareDay]);
    }

    //分享获奖记录
    getSharePrizeDrawLog(startTime, endTime, startIndex, limitSize, userID, channel) {
        return this.db.x_query("call bg_share_prize_draw_log(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, userID, channel]);
    }

    //分享获奖统计
    getSharePrizeDrawStat(startTime, endTime, startIndex, limitSize, type, channel) {
        return this.db.x_query("call bg_share_prize_draw_stat(?,?,?,?,?,?)", [startTime, endTime, startIndex, limitSize, type, channel]);
    }

    //分享访问记录统计
    getWXShareVisitStat(startTime, endTime, startIndex, limitSize) {
        return this.db.x_query("call bg_share_visit_stat(?,?,?,?)", [startTime, endTime, startIndex, limitSize]);
    }

    //任意分享访问记录统计
    getAnyShareVisitStat(startTime, endTime, startIndex, limitSize) {
        return this.db.x_query("call bg_any_share_visit_stat(?,?,?,?)", [startTime, endTime, startIndex, limitSize]);
    }

    //分享奖励记录统计
    getShareRewardStat(startTime, endTime, startIndex, limitSize) {
        return this.db.x_query("call bg_share_reward_stat(?,?,?,?)", [startTime, endTime, startIndex, limitSize]);
    }

    //获取夺宝记录
    getDuoBaoLog(startTime, endTIme, startIndex, limitSize, userID) {
        return this.db.x_query("call bg_duo_bao_log(?,?,?,?,?)", [startTime, endTIme, startIndex, limitSize, userID]);
    }

    //获取宝物记录
    getDuoBaoPrizeLog(startIndex, limitSize, shapeID) {
        return this.db.x_query("call bg_duo_bao_prize_log(?,?,?)", [startIndex, limitSize, shapeID]);
    }

    //获取充值天天送记录
    getRechargeRebateLog(startTime, endTIme, startIndex, limitSize, userID) {
        return this.db.x_query("call bg_activity_recharge_rebate_log(?,?,?,?,?)", [startTime, endTIme, startIndex, limitSize, userID]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////

    //查询被推荐人列表
    getBeRecommendUser(startIndex, limitSize) {
        return this.db.x_query("call bg_be_recommend_user(?,?)", [startIndex, limitSize]);
    }

    //推荐玩家排行
    getRecommendSumRank(startIndex, limitSize) {
        return this.db.x_query("call bg_recommend_sum_rank(?,?)", [startIndex, limitSize]);
    }

    //推荐收入排行
    getRecommendIncomeRank(startTime, endTime, startIndex, limitSize) {
        return this.db.x_query("call bg_recommend_income_rank(?,?,?,?)", [startTime, endTime, startIndex, limitSize]);
    }

    //得到推荐的人数
    getRecommendNum(gameID) {
        return this.db.x_query("select count(*) as num from ps_account where recommendGameID = ?", [gameID]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////


    clearMiBao(userID) {
        return this.db.x_query("UPDATE `ps_account` SET `question1`=NULL, `answer1`=NULL, `question2`=NULL, `answer2`=NULL WHERE `userID`=?", [userID]);
    }

    clearPhone(userID) {
        return this.db.x_query("UPDATE `ps_account` SET phone=null,moorMachine=0 WHERE `userID`=?", [userID]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////

    //获取实名奖励记录
    getRealnameRewardLog(startTime, endTIme, startIndex, limitSize) {
        return this.db.x_query("call bg_user_realname_reward_log(?,?,?,?)", [startTime, endTIme, startIndex, limitSize]);
    }

    //获取有奖推广记录
    getWxSubscribeLog(startTime, endTIme, startIndex, limitSize) {
        return this.db.x_query("call bg_wx_subscribe_log(?,?,?,?)", [startTime, endTIme, startIndex, limitSize]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////
    //获取活动红包领取记录
    getActivityHongBaoTakeLog(startTime, endTIme, startIndex, limitSize, userID) {
        return this.db.x_query("call bg_activity_hong_bao_take_log(?,?,?,?,?)", [startTime, endTIme, startIndex, limitSize, userID]);
    }

    //获取活动红包记录
    getActivityHongBaoLog(startTime, endTIme, startIndex, limitSize, id) {
        return this.db.x_query("call bg_activity_hong_bao_log(?,?,?,?,?)", [startTime, endTIme, startIndex, limitSize, id]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////

    //获取充值类型的图表信息
    getChartDataPayType(startTime, endTime) {
        return this.db.x_query("select  sum(payRMB) as data, payName as label from ps_pay where (ts between ? and ?) and status = 300 group by payName; ", [startTime, endTime]);
    }

    //获取充值渠道的图表信息
    getChartDataPayChannel(startTime, endTime) {
        return this.db.x_query("select  sum(payRMB) as data, channel as label from ps_pay where (ts between ? and ?) and status = 300 group by channel; ", [startTime, endTime]);
    }

    //获取登录渠道的图表信息
    getChartDataLoginChannel(startTime, endTime) {
        return this.db.x_query("select  count(loginID) as data, channel as label from ps_user_login_log where (loginTs between ? and ?)  group by channel; ", [startTime, endTime]);
    }

    //获取微信分享渠道的图表信息
    getChartDataWXShareChannel(startTime, endTime) {
        return this.db.x_query("select  count(id) as data, channel as label from sa_wx_share_order where (createTime between ? and ?)  group by channel; ", [startTime, endTime]);
    }

    //获取转账渠道的图表信息
    getChartDataBusinessChannel(startTime, endTime) {
        return this.db.x_query("select  count(id) as data, channel as label from ps_money_detail where (ts between ? and ?) and type = 7 group by channel; ", [startTime, endTime]);
    }

    //获取在线渠道的图表信息
    getChartDataOnlineChannel(startTime, endTime) {
        return this.db.x_query("select  count(loginID) as data, channel as label from ps_user_login_log where (loginTs between ? and ?) and logoutTs = 0  group by channel; ", [startTime, endTime]);
    }

    //获取在线系统的图表信息
    getChartDataOnlineOS(startTime, endTime) {
        return this.db.x_query("select  count(loginID) as data, os as label from ps_user_login_log where (loginTs between ? and ?) and logoutTs = 0  group by os; ", [startTime, endTime]);
    }

    //获取注册系统的图表信息
    getChartDataRegOS(startTime, endTime) {
        return this.db.x_query("select  count(userID) as data, os as label from ps_reg_log where (ts between ? and ?)   group by os; ", [startTime, endTime]);
    }

    //获取注册系统的图表信息
    getChartDataRegChannel(startTime, endTime) {
        return this.db.x_query("select  count(userID) as data, channel as label from ps_reg_log where (ts between ? and ?)   group by channel; ", [startTime, endTime]);
    }


////////////////////////////////////////////////////////////////////////////////////////////////////////
    //大厅错误网络消息
    getBadPlazaNetMessageLog(startTime, endTime, startIndex, limitSize, userID, type) {
        return this.db.x_query("call bg_bad_plaza_net_message_log(?,?,?,?,?,?);", [startTime, endTime, startIndex, limitSize, userID, type]);
    }

    //错误的大厅写分记录
    getBadWriteScoreLog(startTime, endTime, startIndex, limitSize, userID, roomID) {
        return this.db.x_query("call bg_bad_write_score_log(?,?,?,?,?,?);", [startTime, endTime, startIndex, limitSize, userID, roomID]);
    }

    //错误的大厅写分记录
    getBadSubgameLog(startTime, endTime, startIndex, limitSize, roomID) {
        return this.db.x_query("call bg_bad_subgame_log(?,?,?,?,?);", [startTime, endTime, startIndex, limitSize, roomID]);
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////////////

    //保存微信消息
    saveWXMessage(toUserName, fromUserName, createTime, msgType, msgID, content) {
        return this.db.x_query("insert into bg_wx_message(toUser, fromUser, createTime, msgType, msgID, content) values(?,?,?,?,?,?)", [toUserName, fromUserName, createTime, msgType, msgID, content]);
    }

    //查询微信消息
    selectWXMessage(startTime, endTime, toUserName, fromUserName, msgType, msgID, limitIndex, limitSize) {
        return this.db.x_query("call bg_wx_select_message(?,?,?,?,?,?,?,?)", [startTime, endTime, toUserName, fromUserName, msgType, msgID, limitIndex, limitSize]);
    }

    //查询微信消息
    selectWXMessageLog(id, limitIndex, limitSize) {
        return this.db.x_query("call bg_wx_message_log(?,?,?)", [id, limitIndex, limitSize]);
    }

    //得到微信信息
    getWXMessage(id) {
        return this.db.x_query("select fromUser, toUser from bg_wx_message where id = ?", [id]);
    }

    //获取微信会话
    getWXSession(startTime, endTime, limitIndex, limitSize) {
        return this.db.x_query("call bg_wx_session(?,?,?,?)", [startTime, endTime, limitIndex, limitSize]);
    }

    //封禁所有设备ID登录的账号
    banUserByDeviceId(deviceID, sure) {
        return this.db.x_query("call tools_batch_banUserByDeviceID(?, ?)", [deviceID, sure]);
    }

    //封禁ip当前在线的所有玩家
    banUserByOnlineIp(ip, sure) {
        return this.db.x_query("call tools_batch_banUserByOnlineIP(?, ?)", [ip, sure]);
    }

    getRoleLink(adminid){

        return this.db.x_query("call bg_getRoleLink(?)", [adminid]);
    }
    
    updateAdminPass(userid,oldpass,newpass){
        return this.db.x_query("call bg_updateAdminPass(?,?,?)", [userid,oldpass,newpass]);
    }
    getPryxyList(startIndex, size){
        return this.db.x_query("call bg_proxy_list(?,?)",[startIndex, size]);
    }
    //更新用户的OPENID，用于另一个公众号发放红包
    updateUnionid(unionid,openid)
    {
        return this.db.x_query("update ps_account set `hongbao_openID` = ? where `wxUnionID` = ?" , [openid,unionid]);
    }

////////////////////////////////////////////////////////////////////////////////////////////////////////
}























